package com.johnny.cms.core.controller.nob;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import au.com.bytecode.opencsv.CSVWriter;

import com.johnny.cms.core.service.CmsNOBService;
import com.johnny.core.helper.JsonHelper;
import com.johnny.core.hibernate.orm.Page;
import com.johnny.core.web.JsonMessage;
import com.johnny.core.web.Webs;
import com.johnny.core.web.Webs.ContentType;
import com.johnny.core.web.controller.ControllerSupport;
import com.johnny.core.web.filter.JqGridFilter;
import com.johnny.nob.domain.NboContact;
import com.johnny.nob.domain.NboMailList;

@Controller(value = "nboMailC")
@RequestMapping("/nbo-mail")
public class NobMailListController extends ControllerSupport {
	@Autowired
	private CmsNOBService cmsNOBService;

	@RequestMapping(value = "/list/", method = GET)
	public String list(Model model) {

		return listView();
	}

	public static NboMailList nboMailList = new NboMailList();

	@RequestMapping(value = "/{filter}/views/", method = GET)
	public void views(@PathVariable("filter") String id,
			Page<NboMailList> page, HttpServletResponse response) {
		JqGridFilter jqGridFilter = JsonHelper.fromJsonString(id,
				JqGridFilter.class);

		cmsNOBService.findNobMailListByPage(page, jqGridFilter.getRules(
				nboMailList, null));
		writeJson(response, JsonHelper.toJsonString(page, null, "father",
				"articleTemplate", "notEmptyFlashes", "photos", "firstPhoto",
				"notEmptyPhotos", "firstFlash", "flashes", "selfTemplatePath",
				"content"));
	}

	@Override
	protected String getViewPackage() {
		return "nbo-mail-list";
	}

	@RequestMapping(value = "/submit/", method = POST)
	@ResponseBody
	public JsonMessage submit(HttpServletRequest request) {
		NboMailList nboMailList = new NboMailList();
		try {
			bind(request, nboMailList);
			cmsNOBService.createNboMailList(nboMailList);
			return super.onSuccess();
		} catch (Throwable throwable) {
			return super.onFailure("Please check input data");
		}
	}

	@RequestMapping(value = "/generate/")
	public void generate(HttpServletResponse response) throws IOException {
		Webs.prepareDownload(response, "nbo-mail.csv", ContentType.OCTET);
		List<NboMailList> contacts = cmsNOBService.getAllNboMailList();

		CSVWriter writer = new CSVWriter(response.getWriter());
		String[] header = { "mail" };
		writer.writeNext(header);
		String[] data;
		for (NboMailList s : contacts) {
			data = new String[] { s.getMail() };
			writer.writeNext(data);
		}
	}
}
